package com.yanqu.road.server.cmd.activity.snowgarden;

import com.yanqu.road.entity.enums.activity.snowgarden.eSnowGardenPeriodType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SnowGardenProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = CrossProtocol.C_SNOW_GARDEN_VISITED_RESP, desc = "冰雪游园拜访返回")
public class SnowGardenVisitCrossRespCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SnowGardenProto.SnowGardenToCrossVisitRespMsg req = SnowGardenProto.SnowGardenToCrossVisitRespMsg.parseFrom(packet.getBytes());
        ret = req.getRet();
        int rank = -1;
        if (ret == 0 && null != req.getUserActData()) {
            int period = req.getUserActData().getPeriod();
            int rankType = 0;
            if (period == eSnowGardenPeriodType.UPGRADE_PERIOD.getType()) {
                rankType = eGamePlayerEventType.SnowGardenScoreRank.getValue();
            } else if (period == eSnowGardenPeriodType.VISITOR_PERIOD.getType()) {
                rankType = eGamePlayerEventType.SnowGardenSecondScoreRank.getValue();
            }
            CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityNewRankListModel(req.getActivityId(), rankType);
            if (null != rankListModel && rankType != 0) {
                rank = rankListModel.getMyRank(req.getVisitedUserId(), serverId);
            }
        }

        if (req.hasUserActData() && req.hasVisitedUserId()) {
            UserBaseInfo userBaseInfo = PlayerBasePb.parseBaseUserInfo(req.getUserData());
            CrossUserMgr.updateUserBaseInfo(req.getVisitedUserId(), userBaseInfo);
        }


        SnowGardenProto.VisitOtherSnowGardenRespMsg.Builder msg = SnowGardenProto.VisitOtherSnowGardenRespMsg.newBuilder();
        msg.setRet(ret);
        if (ret == 0) {
            msg.setUserData(req.getUserData());
            msg.setOtherUserData(req.getUserActData());
            msg.setVisitResult(req.getVisitResult());
            msg.setRank(rank);
        }
        SnowGardenProto.CrossVisitOtherSnowGardenRespMsg.Builder respMsg = SnowGardenProto.CrossVisitOtherSnowGardenRespMsg.newBuilder();
        respMsg.setRespMsg(msg);
        respMsg.setRecordTime(req.getRecordTime());
        respMsg.setUserId(req.getUserId());
        respMsg.setTargetUserId(req.getVisitedUserId());
//        YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_SNOW_GARDEN_VISITOR_OTHER, msg);
        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_SNOW_GARDEN_CROSS_VISIT_RETURN, respMsg);
        MessageHelper.sendPacket(req.getUserServerId(), req.getUserId(), message);

    }

    @Override
    public void sendData() {
    }
}
